package com.gec.controller;

import com.gec.dao.BookDao;
import com.gec.dao.BookDao1;
import com.gec.dao.BookDao2;
import com.gec.dao.BookDao3;
import com.gec.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//下面这个url地址 它的/是不可以漏 漏写 项目启动不了
@WebServlet("/AddBookServlet1")
public class AddBookServlet1 extends HttpServlet {

    //接受页面提交过来的客房数据 然后把它添加到数据库中
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取页面的form表单提交过来的数据

        String id = request.getParameter("id");
        BookDao bookDao = new BookDao();
        Book books = bookDao.getBookById(id);
        String name = books.name;
        String author = books.author;
        String price = books.price;


        System.out.println(name+author+price);
        //编写sql语句将 form表单的数据添加到数据库表
        String  sql = "insert into book1 values (?,?,?,?)";
        //用一个集合来暂存要添加的数据
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(id);
        objects.add(name);
        objects.add(author);
        objects.add(price);

        //创建bookDao来操作
         BookDao2 bookDao2=new BookDao2();
         bookDao2.deleteBook(id);
         BookDao3 bookDao3=new BookDao3();
         bookDao3.deleteBook(id);
        //执行sql语句 传入数据
        BookDao1 bookDao1=new BookDao1();

        bookDao1.update(sql,objects);
        List<Book> books3 = bookDao3.getBooks();
        //接下来就需要将查询出来的数据转发到jsp页面 index.jsp
        //将数据存放到request作用域中然后转发到页面去
        request.setAttribute("booklist",books3);
        request.getRequestDispatcher("j.jsp").forward(request,response);
        //添加成功以后将页面重定向 客房列表页面 这样子新增的客房信息就会显示出来了

    }
}

